
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

int send_post_request(const char *url, const char *json_data) {
    CURL *curl;
    CURLcode res;
    int result = 0;

    // Initialize CURL
    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();

    if(curl) {
        // Set the URL for the POST request
        curl_easy_setopt(curl, CURLOPT_URL, url);

        // Set the POST fields
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_data);

        // Set the headers for the POST request
        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Content-Type: application/json");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

        // Perform the request, res will get the return code
        res = curl_easy_perform(curl);

        // Check for errors
        if(res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
            result = 1;
        }

        // Cleanup
        curl_slist_free_all(headers);
        curl_easy_cleanup(curl);
    } else {
        fprintf(stderr, "Failed to initialize CURL\n");
        result = 1;
    }

    // Cleanup CURL globally
    curl_global_cleanup();

    return result;
}

int main(void) {
    const char *url = "http://192.168.43.245:5000/send_email";
    const char *json_data = "{\"content\":\"<h1>Meeting Reminder</h1><p>Don't forget the meeting tomorrow at 10 AM.</p>\"}";

    int result = send_post_request(url, json_data);

    if(result == 0) {
        printf("POST request sent successfully\n");
    } else {
        printf("Failed to send POST request\n");
    }

    return result;
}
